#include<iostream>
using namespace std;
struct nets{
	string SorC;
	string net;
	int s[5];
	int g;//1 0 -1
	bool f;
	nets(){
		f=true;
	}
}a[1001];
int n,dot,m;
int x[1001],x1;
char vis[1001];
bool Sflag=true;
int calc(string s){
	int num=0;
	for(int i=0;i<s.size();i++){
		num=num*10+(s[i]-'0');
	}
	return num;
}
void sub(string c,int ind){
	int start=0,k,len;
	char ch='.';
	for(int i=0;i<5;i++){
		if(i==3) ch=':';
		k=c.find(ch,start);
		if(c[k]=='.') dot++;
		else if(c[k]==':') m++;
		len=k-start;
		if(dot>3||m>1){
			a[ind].g=-1;
			a[ind].f=false;
			return;
		}
		a[ind].s[i]=calc(c.substr(start,len));
		if(a[ind].s[i]>255){
			a[ind].g=-1;
			a[ind].f=false;
			return;
		}
		start=++k;len=0;
	}
} 
int main(){
	freopen("network.in","r",stdin); 
	freopen("network.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].SorC>>a[i].net;
		if(a[i].SorC=="Server") vis[i]='S';
		else vis[i]='C';
		sub(a[i].net,i);
	}
	for(int i=1;i<=n;i++){
		if(vis[i]=='S'&&Sflag&&a[i].f){
			a[i].g=1;
			a[i].f=false;
			x[++x1]=i;
			Sflag=false;
		}
		else if(vis[i]='S'){
			for(int j=1;j<=x1;j++)
				for(int k=0;k<5;i++){
					if(a[i].s[k]!=a[x[j]].s[k]){
						a[i].g=1;
						a[i].f=false;
					}
				}
			if(a[i].f=true){
				a[i].g=0;
				a[i].f=false;
			}
		}
		else if(vis[i]='C'){
			for(int j=1;j<n;j++){
				if(vis[j]=='S'){
					for(int k=0;k<5;k++)
						if(a[j].s[k]!=a[i].s[k]){
							a[i].g=0;
							a[i].f=false;
						}
				}
				if(a[i].f=true){
					a[i].g=j;
					a[i].f=false;
				}
			}
		}
	}
	for(int i=1;i<=n;i++){
		if(a[i].g==-1) cout<<"ERR\n";
		else if(a[i].g==0) cout<<"FAIL\n";
		else{
			if(vis[i]=='S') cout<<"OK\n";
			else cout<<a[i].g<<"\n";
		}
	}
	return 0;
	fclose(stdin);
	fclose(stdout);
}
